package com.cgsd.heyu.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author: sky on 2021/3/22 11:23
 * Email: xcode126@126.com
 * Desc:
 */
public class CommonTools {
    /**
     * 获取总页面
     *
     * @param count    总数
     * @param pagesize 一页查询多少条
     * @return
     */
    public static int getTotalPage(int count, int pagesize) {
        return count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
    }

    /**
     * //null=未接单,0=待完成,1=已提交,2=已审核
     *
     * @param state
     * @return
     */
    public static String getStatus(String state) {
        if (TextUtils.isEmpty(state)) {
            return "未接单";
        }
        switch (state) {
            case "0":
                return "待完成";
            case "1":
                return "已提交";
            case "2":
                return "已审核";
            default:
                return "未接单";
        }
    }

    /**
     * 限制0:不限,1:安卓,2:IOS
     *
     * @param state
     * @return
     */
    public static String getLimitStr(String state) {
        switch (state) {
            case "1":
                return "安卓";
            case "2":
                return "IOS";
            default:
                return "不限";
        }
    }

    /**
     * 用来测试使用的数据
     *
     * @return
     */
    public static List<String> getTestList() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        return list;
    }

    /**
     * 格式化好的日期转成时间戳
     *
     * @param time
     * @return
     */
    public static long dateToLong(String time) {
        if (TextUtils.isEmpty(time)) {
            return 0;
        }
        //Date或者String转化为时间戳
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = "1970-01-06 11:45:55";
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            return 0;
        }
        return date.getTime();
    }

    /**
     * Date转化为String
     *
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 秒级别的时间戳转日期格式
     *
     * @param timestamp
     * @return
     */
    public static String getFormatTime(long timestamp) {
        //时间格式,HH是24小时制，hh是AM PM12小时制
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(timestamp * 1000L));
    }

    /**
     * 检查是否安装了xxx
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean checkApkExist(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            ApplicationInfo n = context.getPackageManager()
                    .getApplicationInfo(packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
